package com.qf.fronted.controller;

import com.alibaba.fastjson.JSONObject;
import com.qf.admin.domain.User;
import com.qf.commons.Data;
import com.qf.fronted.domain.Order;
import com.qf.fronted.domain.WebOrder;
import com.qf.fronted.service.IGoodService;
import com.qf.fronted.service.IOrderService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/order",name = "FrontedOrderServlet")
@CrossOrigin("*")
public class OrderController2 {
    private IOrderService orderServiceImpl2;
    private IGoodService goodsServiceImpl;

    public OrderController2(IOrderService orderServiceImpl2, @Qualifier("goodServiceImpl") IGoodService goodsServiceImpl) {
        this.orderServiceImpl2 = orderServiceImpl2;
        this.goodsServiceImpl = goodsServiceImpl;
    }


    @GetMapping("/getOrdersOfOrderNo")
    public List<Order> getOrdersOfOrderNo(String orderNo){
        List<Order> orderList = orderServiceImpl2.getOrdersOfOrderNo(orderNo);

        return orderList;
    }

    @RequestMapping(value = "/deleteOrderByOrderNo", method = RequestMethod.GET)
    public void deleteOrderByOrderNo(HttpServletRequest request,HttpServletResponse response){
        String orderNo = request.getParameter("orderNo");
        orderServiceImpl2.deleteOrderByOrderNo(orderNo);
    }

    @GetMapping("/getOrdersByStatus")
    public List<WebOrder> getOrdersByStatus(HttpServletRequest request, HttpServletResponse response){
        String status = request.getParameter("status");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<WebOrder> webOrderList = orderServiceImpl2.getOrdersByStatus(Integer.parseInt(status), userId);
        return webOrderList;
    }

    @PutMapping("/changeOrderByOrderNo")
    public void changeOrderByOrderNo(HttpServletRequest request,HttpServletResponse response){
        String orderNo = request.getParameter("orderNo");
        orderServiceImpl2.changeOrderByOrderNo(orderNo);
    }

    @GetMapping("/getOrderinfoByOrderNo")
    private void getOrderinfoByOrderNo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderNo = req.getParameter("orderNo");
        List<Order> orderList = orderServiceImpl2.getOrdersOfOrderNo(orderNo);
        BigDecimal totalPrice = new BigDecimal(0);
        for (Order order : orderList) {
            BigDecimal goodPrice = order.getGoodPrice();
            Integer goodNum = order.getGoodNum();
            totalPrice = totalPrice.add(goodPrice.multiply(BigDecimal.valueOf(goodNum)));
        }
    }

    /**
     * 在结算页面，点击 “确认订单”
     */
    @PostMapping("/ensureOrder")
    private Data ensureOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取商品的id以及商品的数量: {'1001':1, '1002':1}
        String shoppingCartData =  req.getParameter("goodsInfo");
        // 默认的收货地址id
        String takeDelivertyAddressId = req.getParameter("takeDelivertyAddressId");

        // 将json类型的字符串反序列化为 Java对象
        Map<String, Integer> goodsInfo = JSONObject.parseObject(shoppingCartData, Map.class);

        Map<Integer, Integer> goodInfos = new HashMap<>();
        goodsInfo.forEach((k, v) -> {
            goodInfos.put(Integer.parseInt(k), v);
        });

        HttpSession session = req.getSession();

        User user = (User)session.getAttribute("user");

        Integer userId = user.getId();
        //获取用户id

        Data data = orderServiceImpl2.ensureOrder(goodInfos, Integer.parseInt(takeDelivertyAddressId), userId);

//        sendJson(resp, data);
        return data;
    }
}
